package com.gc.web.controller.business;

import com.gc.business.domain.ElevatorTiring;
import com.gc.business.service.IElevatorTiringService;
import com.gc.common.annotation.Log;
import com.gc.common.base.Result;
import com.gc.common.enums.BusinessType;
import com.gc.common.support.Convert;
import com.gc.common.utils.StringUtils;
import com.gc.common.utils.poi.ExcelUtil;
import com.gc.framework.web.base.BaseController;
import com.gc.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * 电梯困人管理 控制层
 * 
 * @author hruiyan
 * @date 2020-11-16
 */
@Slf4j
@Controller
@RequestMapping("/business/elevatorTiring")
public class ElevatorTiringController extends BaseController {
    private String prefix = "business/elevatorTiring";

	@Autowired
	private ISysUserService userService;
	
	private final IElevatorTiringService elevatorTiringService;

	@Autowired
	public ElevatorTiringController(IElevatorTiringService elevatorTiringService) {
		this.elevatorTiringService = elevatorTiringService;
	}

	/**
	*  列表页
	*/
	@RequiresPermissions("business:elevatorTiring:view")
	@GetMapping
	public String elevatorTiring(){
	    return prefix + "/elevatorTiring";
	}

	/**
	 *  历史困人列表页
	 */
	@RequiresPermissions("business:elevatorTiring:view")
	@GetMapping("/historyTiring")
	public String historyTiring(){
		return prefix + "/historyTiring";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(){
		return prefix + "/add";
	}

	/**
	 * 历史困人详情
	 */
	@GetMapping("/historyTiringInfo/{tiringId}")
	public String elevatorBaseInfo(@PathVariable("tiringId") Long tiringId, ModelMap mmap) {
		ElevatorTiring elevatorTiring = elevatorTiringService.getById(tiringId);
		//困人类型(0:人工上报;1:非平层停靠困人;2:不开门困人)
		if (StringUtils.isNotEmpty(elevatorTiring.getTiringType())){
			if (elevatorTiring.getTiringType().equals("0")){
				elevatorTiring.setTiringType("人工上报");
			}else if (elevatorTiring.getTiringType().equals("1")){
				elevatorTiring.setTiringType("非平层停靠困人");
			}else if (elevatorTiring.getTiringType().equals("2")){
				elevatorTiring.setTiringType("不开门困人");
			}
		}
		//状态（0.待确认，1.确认完成）
		if (StringUtils.isNotEmpty(elevatorTiring.getTiringState())){
			if (elevatorTiring.getTiringState().equals("0")){
				elevatorTiring.setTiringState("待确认");
			}else if (elevatorTiring.getTiringState().equals("1")){
				elevatorTiring.setTiringState("确认完成");
			}
		}
		mmap.put("historyTiringInfo", elevatorTiring);
		return prefix + "/historyTiringInfo";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{tiringId}")
	public String edit(@PathVariable("tiringId") Long tiringId, ModelMap mmap){
		ElevatorTiring elevatorTiring = elevatorTiringService.getById(tiringId);
		mmap.put("elevatorTiring", elevatorTiring);
		return prefix + "/edit";
	}
	
	/**
	 * 查询电梯困人管理列表
	 */
	@RequiresPermissions("business:elevatorTiring:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(ElevatorTiring elevatorTiring){

		Long attrId =  getAttrId();
		if(userService.ifCompanyRole(attrId)){
			elevatorTiring.setCompanyId(attrId);
			elevatorTiring.setTiringState("0");
			elevatorTiring.setIsNowDay("1");
			startPage();
			List<ElevatorTiring> list = elevatorTiringService.selectElevatorTiringList(elevatorTiring);
			return Result.success(getDataTable(list));
		}else{
			return Result.success(getDataTable(new ArrayList<ElevatorTiring>()));
		}
	}

	/**
	 * 查询电梯困人管理列表
	 */
	@RequiresPermissions("business:elevatorTiring:list")
	@PostMapping("/historyTiringInfoList")
	@ResponseBody
	public Result historyTiringInfoList(ElevatorTiring elevatorTiring){

		Long attrId =  getAttrId();
		if(userService.ifCompanyRole(attrId)){
			elevatorTiring.setCompanyId(attrId);
			startPage();
			List<ElevatorTiring> list = elevatorTiringService.selectElevatorTiringList(elevatorTiring);
			//移除上报困人
			/*for(Iterator<ElevatorTiring> it = list.iterator(); it.hasNext();) {
				if(it.next().getTiringState().equals("0")){
					it.remove();
				}
			}*/
			return Result.success(getDataTable(list));
		}else{
			return Result.success(getDataTable(new ArrayList<ElevatorTiring>()));
		}
	}

	/**
	 * 新增保存电梯困人管理
	 */
	@RequiresPermissions("business:elevatorTiring:add")
	@Log(title = "电梯困人管理", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(ElevatorTiring elevatorTiring){
		elevatorTiringService.save(elevatorTiring);
		return Result.success();
	}

	/**
	 * 修改保存电梯困人管理
	 */
	@RequiresPermissions("business:elevatorTiring:edit")
	@Log(title = "电梯困人管理", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(ElevatorTiring elevatorTiring){
		elevatorTiringService.updateById(elevatorTiring);
		return Result.success();
	}
	
	/**
	 * 删除电梯困人管理
	 */
	@RequiresPermissions("business:elevatorTiring:remove")
	@Log(title = "电梯困人管理", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		elevatorTiringService.removeByIds(Arrays.asList(longIds));
		return Result.success();
	}

	/**
	 * 导出电梯困人管理列表
	 */
	@RequiresPermissions("business:elevatorTiring:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(ElevatorTiring elevatorTiring){
		List<ElevatorTiring> list = elevatorTiringService.list(elevatorTiring);
		ExcelUtil<ElevatorTiring> util = new ExcelUtil<>(ElevatorTiring.class);
		util.exportExcel(list, "elevatorTiring");
		return Result.success();
	}
}
